package com.example.basic.web.controller.device;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basic.core.result.Result;
import com.example.basic.core.result.ResultGenerator;
import com.example.basic.custom.Log;
import com.example.basic.web.model.device.Device;
import com.example.basic.web.service.device.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@Api(value  = "应用终端管理", tags = {"应用终端管理"})
@RestController
@RequestMapping("/api/v1/byg/device")
public class DeviceController {

    @Resource
    private DeviceService deviceService;

    @Log(title = "应用终端管理", description = "新增或修改")
    @ApiOperation(value = "新增或修改", notes = "新增或修改")
    @PostMapping("/addOrUpdate")
    public Result<Device> addOrUpdate(@ApiParam(name = "device", value = "模型", required = true) @RequestBody Device device) {
        return ResultGenerator.genSuccessResult(deviceService.addOrUpdate(device));
    }

    @Log(title = "应用终端管理", description = "详情")
    @ApiOperation(value = "详情", notes = "详情")
    @PostMapping("/detail")
    public Result<Device> detail(@ApiParam(name = "devicePoint", value = "应用终端编码") @RequestParam String devicePoint) {
        return ResultGenerator.genSuccessResult(deviceService.detail(devicePoint));
    }

    @Log(title = "应用终端管理", description = "删除")
    @ApiOperation(value = "删除", notes = "删除")
    @PostMapping("/delete")
    public Result<Integer> delete(@ApiParam(name = "devicePoint", value = "应用终端编码") @RequestParam String devicePoint ) {
        return ResultGenerator.genSuccessResult(deviceService.delete(devicePoint));
    }

    @Log(title = "应用终端管理", description = "启用/禁用")
    @ApiOperation(value = "启用/禁用", notes = "启用/禁用")
    @PostMapping("/enableOrDisable")
    public Result<Integer> enableOrDisable(@ApiParam(name = "devicePoint", value = "应用终端编码") @RequestParam String devicePoint) {
        return ResultGenerator.genSuccessResult(deviceService.enableOrDisable(devicePoint));
    }

    @Log(title = "应用终端管理", description = "列表")
    @ApiOperation(value = "列表", notes = "列表")
    @PostMapping("/queryList")
    public Result<IPage<Device>> queryList(
            @ApiParam(name = "deviceType", value = "设备类型 0-全部") @RequestParam(defaultValue = "0") String deviceType,
            @ApiParam(name = "isAll", value = "是否查询全部 0-否，1-是") @RequestParam(defaultValue = "0") String isAll,
            @ApiParam(name = "status", value = "设备状态 0-全部") @RequestParam(defaultValue = "0") String status,
            @ApiParam(name = "page", value = "页数，list分页的第几页") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "条数，list分页的每页条数") @RequestParam(defaultValue = "0") Integer size) {
        Page<Device> queryPage = new Page<>(page,size);
        return ResultGenerator.genSuccessResult(deviceService.queryList(queryPage,deviceType, isAll,status));
    }

    @GetMapping("/queryListByType")
    public Result<List<Device>> queryListByType(String deviceType) {
        return ResultGenerator.genSuccessResult(deviceService.queryListByType(deviceType));
    }
}
